package com.icesoft.system.controller;

import com.icesoft.framework.core.controller.BaseCrudRestController;
import com.icesoft.framework.core.vo.JsonResult;
import com.icesoft.framework.core.vo.Pagination;
import com.icesoft.system.dto.OrgDTO;
import com.icesoft.system.entity.Org;
import com.icesoft.system.permission.OperationCons;
import com.icesoft.system.permission.annotation.BindPermission;
import com.icesoft.system.service.impl.OrgServiceImpl;
import com.icesoft.system.vo.OrgVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 组织机构Controller
 *
 * @author ${author}
 * @date 230326
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@BindPermission(name = "组织机构管理")
@RequestMapping("/system/org")
public class OrgController extends BaseCrudRestController<Org> {

	private final OrgServiceImpl orgService;

	/**
	 * 查询ViewObject的分页数据
	 * <p>
	 * url请求参数示例: /list?field=abc&pageSize=20&pageIndex=1&orderBy=id
	 * </p>
	 *
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/query")
	@BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
	public JsonResult getViewObjectListMapping(OrgDTO entity, Pagination pagination) throws Exception {
		return super.getViewObjectList(entity, pagination, OrgVO.class);
	}

	/**
	 * 根据资源id查询ViewObject
	 *
	 * @param id ID
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/{id}")
	@BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
	public JsonResult getViewObjectMapping(@PathVariable("id") Long id) throws Exception {
		return super.getViewObject(id, OrgVO.class);
	}

	/**
	 * 新建
	 *
	 * @param dto
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/create")
	@BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
	public JsonResult createEntityMapping(@Valid @RequestBody OrgDTO dto) throws Exception {
		return super.createEntity(dto);
	}

	/**
	 * 更新
	 *
	 * @param dto
	 * @return JsonResult
	 * @throws Exception
	 */
	@PostMapping("/update/{id}")
	@BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
	public JsonResult updateEntityMapping(@PathVariable("id") Long id, @Valid @RequestBody OrgDTO dto) throws Exception {
		return super.updateEntity(id, dto);
	}

	/**
	 * 根据id删除资源对象
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/delete/{id}")
	@BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
	public JsonResult deleteEntityMapping(@PathVariable("id") Long id) throws Exception {
		return super.deleteEntity(id);
	}

	@GetMapping("tree")
	@BindPermission(name = "获取组织机构树", code = OperationCons.CODE_READ)
	public JsonResult tree() throws Exception {
		return JsonResult.OK(orgService.getOrgTree());
	}
}
